/*
 * Chargement.java
 *
 * Created on 22 novembre 2007, 17:47
 */

package ihm;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author  port
 */
public class Chargement extends javax.swing.JDialog {
    
    /** Creates new form Chargement */
	IhmInterface ihm;
	File fichier1;
	File fichier2;
	
    public Chargement(IhmInterface ihm) {
    	this.ihm = ihm;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    
    private void fermer(){
    	this.dispose();
    }
    
    private void valider(){
    	if (fichier1 != null && fichier2 != null){
    		ihm.setFichierDeDonnees(fichier1);
    		ihm.setFichierDeFrontiere(fichier2);
    		this.dispose();
    	}else{
    		JOptionPane.showMessageDialog(this, "Vous devez renseigner les deux noms de fichier");
    	}
    	
    }
    
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setTitle("Projet de programmation stochastique");
        jButton1.setText("Ouvrir");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            	getFichier(fichier1);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 60, -1, -1));

        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 60, 200, -1));

        getContentPane().add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 130, 200, -1));

        jLabel1.setText("Veuillez choisir le fichier de données du projet :");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 30, 380, 20));

        jLabel2.setText("Veuillez choisir le fichier représentant la frontière optimale (optionnel) :");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 100, 540, 20));

        jButton4.setText("Charger");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            	valider();
            }
        });
        getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 180, -1, -1));

        jButton2.setText("Annuler");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            	fermer();
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 180, -1, -1));

        jButton3.setText("Ouvrir");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            	getFichier(fichier2);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 130, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    

    private void getFichier(File f){
	    JFileChooser fC = new JFileChooser(new File("./exemples"));
		fC.setMultiSelectionEnabled(true);
		fC.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
	
		if (fC.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			f = new File(fC.getSelectedFile().getAbsolutePath());
			if (!f.getName().contains("_FE")){
				this.jTextField1.setText(f.getName());
				fichier1 = f;
			}else{
				this.jTextField2.setText(f.getName());
				fichier2 = f;
			}
		}
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
    
}
